package com.phenix.mvp;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;

import java.util.Stack;

/**
 * Created by zhouphenix on 2016-7-25.
 * @author zhouphenix
 */

public class App extends Application {
    /**App*/
    private static App APP;
    /**Activities stack*/
    private Stack<Activity> mActivities;


    public static App getAppContext(){
        return APP;
    }

    public Activity getCurrentActivity(){
        return mActivities.lastElement();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        APP = this;
        mActivities = new Stack<>();

        Logger.init("phenix")                       // default PRETTYLOGGER or use just init()
                .logAdapter(new AndroidLogAdapter())//default AndroidLogAdapter
                .logLevel(LogLevel.FULL)            // default LogLevel.FULL
                .methodCount(5)                     // default 2
                .methodOffset(2);                   // default 0

//        AppCompatDelegate.setDefaultNightMode(SpUtil.isNight() ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
        registerActivityLifecycleCallbacks(new MyActivityLifecycle());
    }



    private class MyActivityLifecycle implements ActivityLifecycleCallbacks {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            mActivities.add(activity);
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            mActivities.remove(activity);
        }
    }
}
